import { defineConfig, loadEnv } from "vite";
import vue from "@vitejs/plugin-vue";
import AutoImport from "unplugin-auto-import/vite";
import Components from "unplugin-vue-components/vite";
import { VantResolver } from "@vant/auto-import-resolver";

export default defineConfig(({ command, mode }) => {
  //检查process.cwd()路径下.env.development.local、.env.development、.env.local、.env这四个环境文件
  const env = loadEnv(mode, process.cwd());
  //获取环境变量中的值
  console.log(env.VITE_APP_URL_BASE, "真实地址");

  const HYBHAPI = env.VITE_APP_API_BASE;
  const HYBHURL = env.VITE_APP_URL_BASE;

  //返回vite配置
  return {
    base: './',
    plugins: [
      vue(),
      AutoImport({
        resolvers: [VantResolver()],
      }),
      Components({
        resolvers: [VantResolver()],
      }),
    ],
    server: {
      proxy: {
        [HYBHAPI]: {
          target: HYBHURL, //目标域名
          changeOrigin: true, //需要代理跨域
          rewrite: (path) => path.replace(HYBHAPI, ''), //路径重写，把'/api'替换为''
        },
      },
    },
  }
})
